package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import service.DoctorfeeService;
import pojo.Doctorfee;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 医生收费项目管理API
 */
@Api(tags = "医生收费项目管理", description = "提供医生收费项目的增删改查服务")
@CrossOrigin
@RestController
@RequestMapping("/doctorfees")
public class DoctorfeeController {

    @Autowired
    private DoctorfeeService doctorfeeService;

    /**
     * 添加新的医生收费项目
     * @param doctorfee 收费项目实体，包含项目名称、价格、状态等信息
     * @return 操作结果，包含状态码、消息和新增的收费项目信息
     */
    @ApiOperation(value = "新增医生收费项目", notes = "创建新的医生收费项目记录，返回操作结果和新增的项目信息")
    @ApiImplicitParam(name = "doctorfee", value = "收费项目实体", required = true, dataType = "Doctorfee", paramType = "body")
    @PostMapping("/add")
    public ResponseEntity<Map<String, Object>> addDoctorfee(@RequestBody Doctorfee doctorfee) {
        Map<String, Object> response = new HashMap<>();
        int result = doctorfeeService.insertDoctorfee(doctorfee);
        if (result > 0) {
            response.put("status", "success");
            response.put("message", "费用项目添加成功");
            response.put("data", doctorfee);
            return ResponseEntity.status(HttpStatus.CREATED).body(response);
        } else {
            response.put("status", "error");
            response.put("message", "费用项目添加失败");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    /**
     * 根据ID删除医生收费项目
     * @param feeId 收费项目ID
     * @return 操作结果，包含状态码和消息
     */
    @ApiOperation(value = "删除医生收费项目", notes = "根据项目ID删除对应的医生收费项目")
    @ApiImplicitParam(name = "id", value = "收费项目ID", required = true, dataType = "Integer", paramType = "path")
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteDoctorfee(@PathVariable("id") Integer feeId) {
        Map<String, Object> response = new HashMap<>();
        int result = doctorfeeService.deleteDoctorfeeById(feeId);
        if (result > 0) {
            response.put("status", "success");
            response.put("message", "费用项目删除成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("status", "error");
            response.put("message", "费用项目删除失败，ID不存在");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 更新医生收费项目信息
     * @param doctorfee 收费项目实体，包含需要更新的信息
     * @return 操作结果，包含状态码、消息和更新后的项目信息
     */
    @ApiOperation(value = "更新医生收费项目", notes = "根据项目ID更新医生收费项目的信息，如名称、价格、状态等")
    @ApiImplicitParam(name = "doctorfee", value = "收费项目实体", required = true, dataType = "Doctorfee", paramType = "body")
    @PutMapping("/update")
    public ResponseEntity<Map<String, Object>> updateDoctorfee(@RequestBody Doctorfee doctorfee) {
        Map<String, Object> response = new HashMap<>();
        int result = doctorfeeService.updateDoctorfee(doctorfee);
        if (result > 0) {
            response.put("status", "success");
            response.put("message", "费用项目更新成功");
            response.put("data", doctorfee);
            return ResponseEntity.ok(response);
        } else {
            response.put("status", "error");
            response.put("message", "费用项目更新失败，ID不存在");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 根据ID查询医生收费项目详情
     * @param feeId 收费项目ID
     * @return 操作结果，包含状态码和项目详细信息
     */
    @ApiOperation(value = "查询医生收费项目详情", notes = "根据项目ID获取医生收费项目的详细信息")
    @ApiImplicitParam(name = "id", value = "收费项目ID", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getDoctorfee(@PathVariable("id") Integer feeId) {
        Map<String, Object> response = new HashMap<>();
        Doctorfee doctorfee = doctorfeeService.selectDoctorfeeById(feeId);
        if (doctorfee != null) {
            response.put("status", "success");
            response.put("data", doctorfee);
            return ResponseEntity.ok(response);
        } else {
            response.put("status", "error");
            response.put("message", "费用项目不存在");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 查询所有医生收费项目列表
     * @return 操作结果，包含状态码、总数和项目列表
     */
    @ApiOperation(value = "获取所有医生收费项目", notes = "获取系统中所有医生收费项目的列表")
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> getAllDoctorfees() {
        Map<String, Object> response = new HashMap<>();
        List<Doctorfee> doctorfees = doctorfeeService.selectAllDoctorfees();
        response.put("status", "success");
        response.put("total", doctorfees.size());
        response.put("data", doctorfees);
        return ResponseEntity.ok(response);
    }

    /**
     * 根据状态查询医生收费项目列表
     * @param status 状态码（1=启用，0=禁用）
     * @return 操作结果，包含状态码、总数和符合条件的项目列表
     */
    @ApiOperation(value = "根据状态查询医生收费项目", notes = "根据状态码查询对应的医生收费项目列表")
    @ApiImplicitParam(name = "status", value = "状态码（1=启用，0=禁用）", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("/status/{status}")
    public ResponseEntity<Map<String, Object>> getDoctorfeesByStatus(@PathVariable("status") Integer status) {
        Map<String, Object> response = new HashMap<>();
        List<Doctorfee> doctorfees = doctorfeeService.selectDoctorfeesByStatus(status);
        response.put("status", "success");
        response.put("total", doctorfees.size());
        response.put("data", doctorfees);
        return ResponseEntity.ok(response);
    }
    /**
     * 根据医生ID查询号别和挂号费
     * @param doctorId 医生ID
     * @return 操作结果，包含状态码和号别、挂号费信息
     */
    @ApiOperation(value = "根据医生ID查询号别和挂号费", notes = "根据医生ID查询号别和挂号费信息")
    @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "String", paramType = "query")
    @GetMapping("/feeInfoByDoctorId")
    public ResponseEntity<Map<String, Object>> getFeeInfoByDoctorId(@RequestParam String doctorId) {
        Map<String, Object> response = new HashMap<>();
        List<Map<String, Object>> data = doctorfeeService.selectFeeInfoByDoctorId(doctorId);
        response.put("status", "success");
        response.put("data", data);
        return ResponseEntity.ok(response);
    }
}